from math import ceil
from scipy.io import wavfile
import pylab as p
import constants as c
import filter as f
import utils as u
import time as t

#------------------------------------------------
# Script Principal
#------------------------------------------------
print("Iniciando comparacion...")

# Cargo la senial a procesar
print("Cargando datos del archivo de audio...")
fs, data = wavfile.read(c.DATA_PATH + c.DATA_NAME)

# Crear el filtro
print("Generando filtro FIR...")
filter_coef = f.createFIRFilter(Fs = fs, FPass = c.FIR_PASS, N = c.FIR_N)

# Cantidad de realimentacion por el filtro
for i in xrange(0, 10):
    SIZE = (2**i) * 1024 - c.FIR_N + 1
    segment_count = int(ceil((len(data) * 1.0) / SIZE))
    print(str(i) + " - Segment Size: " + str(SIZE))
 
    trailing = []    
    filtered_signal = []
        
    # Correr filtrado por CONV
    tic = t.time()
    for j in xrange(segment_count):
        segment = data[j * SIZE : (j + 1) * SIZE]
        filtered_segment, trailing = u.filterSignalConvolution(filter_coef, segment, j, trailing, segment_count)
    toc = t.time()
    print("--Conv:\t" + str(toc-tic) + "s")
    
    trailing = []    
    filtered_signal = []
    filter_fft = []
    
    # Correr filtrado por FFT
    tic = t.time()
    for j in xrange(segment_count):
        segment = data[j * SIZE : (j + 1) * SIZE]
        filtered_segment, trailing, filter_fft = u.filterSignalFFT(filter_coef, segment, j, trailing, segment_count, filter_fft)
    toc = t.time()
    print("--FFT:\t" + str(toc-tic) + "s")
    
tic = t.time()
filtered_segment = p.convolve(data, filter_coef)
toc = t.time()
print("Conv Total:\t" + str(toc-tic) + "s")